/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.navid.trafalgar.mod.windtunnel.statelisteners;

import com.jme3.app.Application;
import com.jme3.input.InputManager;
import com.jme3.input.controls.ActionListener;
import com.navid.trafalgar.manager.EventManager;
import com.navid.trafalgar.manager.GameStatus;
import com.navid.trafalgar.manager.states.PrestartState;
import com.navid.trafalgar.manager.states.StartedState;
import com.navid.trafalgar.model.AShipModel;
import com.navid.trafalgar.model.IContext;
import com.navid.trafalgar.util.FormatUtils;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.screen.Screen;

/**
 *
 * @author alberto
 */
public class GUIUpdater implements StartedState, PrestartState {

    private Screen screen;
    private Nifty nifty;
    private GameStatus gameStatus;
    private final InputManager inputManager;
    private final EventManager eventManager;
    //GUI elements
    private TextRenderer speedStats;
    private TextRenderer windStats;
    private TextRenderer orientationStats;
    private TextRenderer timeStats;
    //Map elements
    private AShipModel ship;
    private IContext iContext;
    private boolean showMenu;

    public GUIUpdater(Screen screen, Nifty nifty, GameStatus gameStatus, EventManager eventManager, Application appSettings) {
        this.screen = screen;
        this.nifty = nifty;
        this.gameStatus = gameStatus;
        this.inputManager = appSettings.getInputManager();
        this.eventManager = eventManager;
    }

    public void onStarted(float tpf) {
        speedStats.setText("speed: " + FormatUtils.formatFloatX_XXX(ship.getSpeed()));
        windStats.setText("wind orientation: " + FormatUtils.formatFloatX_XXX(iContext.getWind().getWind().getAngle()));
        //orientationStats.setText("ship orientation: " + FormatUtils.formatFloatX_XXX(ship.getOrientation()));
        timeStats.setText("Time: " + gameStatus.getTime());
    }

    public void onUnload() {

        speedStats = null;
        windStats = null;
        orientationStats = null;
        timeStats = null;
        inputManager.removeListener(actionListener);
    }

    public void onPrestart(float tpf) {
        ship = gameStatus.getPlayerNode();
        iContext = gameStatus.getMillestoneGameModel().getIContext();

        speedStats = screen.findElementByName("speed_stats").getRenderer(TextRenderer.class);
        windStats = screen.findElementByName("wind_stats").getRenderer(TextRenderer.class);
        orientationStats = screen.findElementByName("orientation_stats").getRenderer(TextRenderer.class);
        timeStats = screen.findElementByName("time").getRenderer(TextRenderer.class);
        showMenu = false;

        inputManager.addListener(actionListener, "Menu"); // load my custom keybinding
        showMenuFunction(false); //nifty keeps the status, we need to reset it.
    }

    public void showMenuFunction(boolean value) {
        screen.findElementByName("menuLayer").setVisible(value);
        showMenu = value;
        if (value) {
            eventManager.fireEvent("PAUSE");
        } else {
            eventManager.fireEvent("RESUME");
        }
    }
    private ActionListener actionListener = new ActionListener() {

        public void onAction(String name, boolean isPressed, float tpf) {
            if (isPressed) {
                if (name.equals("Menu")) {
                    showMenu = !showMenu;
                    showMenuFunction(showMenu);
                }
            }
        }
    };

    public void resume() {
        showMenuFunction(false);
    }

    public void restart() {
        eventManager.fireEvent(EventManager.FAILED);
        nifty.gotoScreen("windTunnelScreen");
    }

    public void quit() {
        eventManager.fireEvent(EventManager.FAILED);
        nifty.gotoScreen("preWindTunnelScreen");
    }
}
